package com.skplanet.rade.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import org.hibernate.annotations.Type;

import com.google.gson.annotations.Expose;

@Entity
@Table(name = "story")
public class Story implements Serializable{
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Expose
	private Long sid;
	
	@Column(name = "writer")
	@Expose
	private String writer;

	@Column(name = "title")
	@Expose
	private String title;
	
	@Column(name = "contents")
	@Type(type="text")
	@Expose
	private String contents;

	@Column(name = "readcount")
	@Expose
	private int readcount;
	
	@Column(name = "regdate")
	@Expose
	private Date regdate;
	

	@Column(name = "share") //"true" or "false"
	@Expose
	private String share;
	
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "user_id")
	private User user;
	
	@OneToMany(mappedBy = "story", fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
	@OrderBy("stid ASC" )
	@Expose
	private List<StoryTag> storyTags;
	
	public Story() {
		setReadcount(0);
		setRegdate(new Date());
	}

	public Long getSid() {
		return sid;
	}

	public void setSid(Long sid) {
		this.sid = sid;
	}

	public String getWriter() {
		return writer;
	}

	public void setWriter(String writer) {
		this.writer = writer;
	}

	public String getTitle() {
		return title;
	}



	public void setTitle(String title) {
		this.title = title;
	}



	public String getContents() {
		return contents;
	}



	public void setContents(String contents) {
		this.contents = contents;
	}



	public int getReadcount() {
		return readcount;
	}



	public void setReadcount(int readcount) {
		this.readcount = readcount;
	}



	public Date getRegdate() {
		return regdate;
	}



	public void setRegdate(Date regdate) {
		this.regdate = regdate;
	}

	public String getShare() {
		return share;
	}

	public void setShare(String share) {
		this.share = share;
	}

	public User getUser() {
		return user;
	}



	public void setUser(User user) {
		this.user = user;
	}



	public List<StoryTag> getStoryTags() {
		return storyTags;
	}



	public void setStoryTags(List<StoryTag> storyTags) {
		this.storyTags = storyTags;
	}

	public void addStoryTag(StoryTag storyTag){
		if(storyTags == null){
			storyTags = new ArrayList<StoryTag>();
		}
			storyTags.add(storyTag);
	}
}
